package com.adcc.swingui.serialize;

import java.io.File;

import org.apache.log4j.Logger;

/**
 * <p>
 * Title: SerializeObjectParams
 * </p>
 * <p>
 * Description: 序列化信息配置
 * </p>
 * <p>
 * Copyright: Copyright (c) 2010
 * </p>
 * 
 * @author jeff.zhao
 * @version 1.0.0
 */
public class SerializeObjectParams {
	
	private static Logger logger = Logger.getLogger(SerializeObjectParams.class);

	/** 对象保存路径 */
	private static String objsavePath = "/serialize";
	/** 子目录名称 */
	private static String subDirectory = null;
	/** 扩展名 */
	private static String extName = ".cfg";

	/** 获取扩展名 */
	public static String getExtname() {
		return extName;
	}

	/**
	 * 设置子目录
	 * @param subDirectory  一般是系统的用户名
	 */
	public static void setSubDirectory(String subDirectory) {

		if (subDirectory != null && !"".equals(subDirectory)) {
			if (SerializeObjectParams.subDirectory == null) {
				if (objsavePath.lastIndexOf("/") != objsavePath.length() - 1) {
					objsavePath = objsavePath + "/";
				}

				objsavePath += subDirectory;
			} else {
				objsavePath = objsavePath.replace(
						SerializeObjectParams.subDirectory, subDirectory);
			}

			SerializeObjectParams.subDirectory = subDirectory;
		}
		//目录不存在创建目录
		try {
			File f = new File(System.getProperty("user.dir") + objsavePath);
			if (!f.exists()) {
			    f.mkdirs();
			}
		} catch (Exception e) {
			logger.error("目录创建失败" + System.getProperty("user.dir") + objsavePath + e.getMessage());
		}
	}

	/** 
	 * 获取对象保存路径 
	 */
	public static String getObjsavePath() {
		objsavePath = objsavePath.replace("\\", "/");
		objsavePath = objsavePath.replace("//", "/");

		if (!objsavePath.startsWith("/")) {
			objsavePath = "/" + objsavePath;
		}

		return objsavePath;
	}

}
